<?php
// $Id: table_hotel.php 434 2012-06-06 00:32:17Z tomqin@gmail.com $

class Table_Hotel_Module{
    private $user = null;

    public function __construct($user){
        $this->user = $user;
    }

    public function check_user_right($shop_id){
        if($this->user->status == USER_ADMIN){
            return true;
        }elseif($this->user->status == USER_BUSINESS){
            $uid = $this->user->uid;
            $shop_id = intval($shop_id);
            return Db::getFirst("SELECT count(shop_id) FROM `biz_shop` WHERE admin_uid = '$uid' AND shop_id = '$shop_id'");
        }else{
            return false;
        }
    }

    public function get_data($shop_id){
        $shop_id = intval($shop_id);
        //$sql  = "SELECT * FROM `biz_shop` s LEFT JOIN `biz_hotel` h ON s.shop_id = h.shop_id WHERE s.shop_id = '{$shop_id}'";
        $sql = "SELECT * FROM `biz_hotel` WHERE shop_id = '{$shop_id}'";
        return Db::getRow($sql);
    }

    public function update($shop_id, array $post){
        $post['shop_id'] = $shop_id;
        $data = $this->hotel_fields($post);
        Db::insert('biz_hotel', $data, true);

        $list_data = array(
            'price'  => $data['price'],
            'person' => $data['person'],
            'table'  => $data['table'],
            'halls'  => Db::getFirst("SELECT COUNT(*) FROM `biz_hotel_hall` WHERE shop_id = '$shop_id'"),
            'menus'  => Db::getFirst("SELECT COUNT(*) FROM `biz_hotel_menu` WHERE shop_id = '$shop_id'"),
            'tags'   => $data['tags'],
        );
        Db::update('biz_shop', array('list_data' => serialize($list_data)), array('shop_id'=> $shop_id));
        return true;
    }

    protected function hotel_fields($post){
        return array(
            'shop_id'    => array_get_int($post, 'shop_id'),
            'description'=> array_get_string($post, 'description'),
            'service'    => array_get_string($post, 'service'),
            'price'      => array_get_int($post, 'min_price').','.array_get_int($post, 'max_price'),
            'table'      => array_get_int($post, 'min_table').','.array_get_int($post, 'max_table'),
            'person'     => array_get_int($post, 'min_person').','.array_get_int($post, 'max_person'),
            'tags'       => join(',', array_get($post, 'tags')),
        );
    }
}
?>